#include "fxcc/core/graph/opengl3/pbrx/HdrSkyboxDebugEffect.h"

using namespace fxcc::graph::opengl3::pbrx;

HdrSkyboxDebugEffect::HdrSkyboxDebugEffect()
    :fxcc::graph::opengl3::SkyboxEffect(HdrSkyboxDebugEffect::FragCode())
{

    m_Shader->Use();
    m_Shader->setInt("environmentMap", 4);
}

void HdrSkyboxDebugEffect::Begin()
{
    fxcc::graph::opengl3::SkyboxEffect::Begin();
}

void HdrSkyboxDebugEffect::RenderSkybox(const fxcc::graph::common::TextureCubeJack* texture)
{
    texture->SetShader(4);
    m_SkyboxMesh->Bind();
    m_SkyboxMesh->DrawElementsAuto();
}

void HdrSkyboxDebugEffect::End()
{
    fxcc::graph::opengl3::SkyboxEffect::End();
}

std::string HdrSkyboxDebugEffect::FragCode()
{
    return R"(
out vec4 FragColor;

uniform samplerCube environmentMap;

void main()
{		

    vec3 envColor = textureLod(environmentMap, localPos, 0.0).rgb;
    
    // HDR tonemap and gamma correct
    envColor = envColor / (envColor + vec3(1.0));
    envColor = pow(envColor, vec3(1.0/2.2)); 
    
    FragColor = vec4(envColor, 1.0);
}
)";
}
